package main

import (
	"database/sql"
	"log"

	// "github.com/go-redis/redis/v8"
	_ "github.com/go-sql-driver/mysql"
)

func getDBConnection() *sql.DB {
	dsn := "root:wy792920@tcp(localhost:3306)/golang"
	db, err := sql.Open("mysql", dsn)
	if err != nil {
		log.Fatal(err)
	}
	return db
}

func MySQLQuery(db *sql.DB, sqlStr string) ([]Student, error) {
	rows, err := db.Query(sqlStr)
	if err != nil {
		return nil, err
	}
	defer rows.Close()

	students := make([]Student, 0)
	for rows.Next() {
		stu1 := Student{}
		if err := rows.Scan(&stu1.Id, &stu1.Name, &stu1.Chinese, &stu1.Math, &stu1.English); err != nil {
			return nil, err
		}
		students = append(students, stu1)
	}

	return students, nil
}
